package com.jd.finance.core.controller.admin;

import com.jd.finance.core.config.URLConst;
import com.jd.finance.core.service.UserService;
import com.jd.finance.core.service.impl.AdminUtilsServiceImpl;
import com.jd.finance.result.BaseResult;
import com.jd.finance.util.HttpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * 后台管理员表
 *
 * @author  dubiao
 */
@Api(tags = "后台管理")
@Slf4j
@RestController
@RequestMapping("/admin/core/adminCenter")
public class AdminCenterController {
    @Autowired
    private UserService userService;

    @Resource
    private AdminUtilsServiceImpl adminUtilsService;

    @ApiOperation("管理员登录")
    @GetMapping("/adminLogin")
    public BaseResult adminLogin(String userPhone, String userPassword,HttpServletRequest request){
        if (StringUtils.isAllBlank(userPassword,userPhone)) {
            log.error("登录信息为空！");
            return BaseResult.error().message("登录信息为空");
        }

        return userService.userAdminLogin(userPassword,userPhone,request);
    }


    @ApiOperation("还款")
    @GetMapping("/javaweb")
    public void test(){

        //修改参数
        HashMap<String, Object> paraMap = new HashMap<>();
        paraMap.put("money","1000");
        paraMap.put("card","88888");
        paraMap.put("uName","eeeeeee");
        paraMap.put("hkId","9");
        //封装 post 参数 到路径
        StringBuilder postData = new StringBuilder();
        for(Map.Entry<String,Object> param : paraMap.entrySet()){
            postData.append(param.getKey()).append("=")
                    .append(param.getValue()).append("&");
        }
        log.info(String.format("--> 发送请求到web：post data %1s", postData));
        byte[] bytes1 = postData.toString().getBytes(StandardCharsets.UTF_8);
        byte[] result = HttpUtils.doPost(URLConst.GET_HUANKAN,bytes1);
        String s = new String(result);
        log.info(String.format("--> web应答结果：result data %1s", s));


    }


    @ApiOperation("根据uid，修改密码")
    @PostMapping("/updatePassword")
    public BaseResult updatePassword(Integer uid, String oldPassword, String newPassword, String checkPassword, HttpServletRequest request) {
        if (StringUtils.isAllBlank(String.valueOf(uid),oldPassword,newPassword,checkPassword)) {
            log.error("根据uid修改密码，提交信息为空!");
            return BaseResult.error().message("根据uid修改密码，提交信息为空!");
        }

        if (!adminUtilsService.isAdmin(uid)) {
            log.error("该用户非管理员身份!");
            return BaseResult.error().message("根据uid修改密码该用户非管理员身份!");
        }
        return userService.updatePassword(uid,oldPassword,newPassword,checkPassword,request);
    }

    @ApiOperation("根据uid,修改手机号")
    @PostMapping("/updatePhone")
    public BaseResult updatePhone(Integer uid,String userPhone,HttpServletRequest request) {
        if (uid==null || userPhone==null) {
            log.error("根据uid修改手机号，提交信息为空!");
            return BaseResult.error().message("根据uid修改手机号，提交信息为空!");
        }
        if (!adminUtilsService.isAdmin(uid)) {
            log.error("该用户非管理员身份!");
            return BaseResult.error().message("根据uid修改密码该用户非管理员身份!");
        }
        return userService.updatePhone(uid,userPhone,request);
    }
}
